<?php

namespace App\Traits;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

trait ProxyLogin
{
    /**
     * 代理进行OAuth登录,对前端隐藏client配置
     * @return mixed
     * @throws
     */
    public function proxyAuthenticate($mobile, $password)
    {
        $client = new Client();
        try {
            $url     = request()->root() . '/api/oauth/token';
            $params  = array_merge(config('passport.proxy'), [
                'username' => $mobile,
                'password' => $password,
            ]);
            $respond = $client->request('POST', $url, ['connect_timeout' => 8, 'form_params' => $params]);
        } catch (RequestException $exception) {
            return false;
        }
        if ($respond->getStatusCode() !== 401) {
            return json_decode($respond->getBody()->getContents(), true);
        }
        return false;
    }
}